package com.cda.service.impl;


import com.cda.api.FaceApi;
import com.cda.common.BaseLogger;
import com.cda.entity.ListRecord;
import com.cda.entity.Person;
import com.cda.service.FaceService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;



@Service("faceService")
public class FaceServiceImpl extends BaseLogger implements FaceService  {


    @Autowired
    FaceApi faceApi;

    @Value("c{face.tipsBefore}")
    String tipsBefore;

    @Value("${face.tipsAfter}")
    String tipsAfter;

    @Value("${face.count}")
    int count;



    @Override
    /**
     * {
     * "msg": "成功",
     * "status": 0,
     * "data": {
     * "photos":["Base64", "Base64", ...]}
     *
     */
    public boolean photograph(Person p) {

        String result= faceApi.photo( tipsBefore,tipsAfter,count );
        System.out.println("【拍照返回数据】"+result);
        JSONObject json = JSONObject.fromObject(result);
        if( 0 == json.getInt("status") ){

            JSONObject data = JSONObject.fromObject(json.get("data"));
            JSONArray photos = JSONArray.fromObject(data.get("photos"));
            for(int i=0;i<photos.size();i++){
                logger.info("【人脸图像比对第"+i+"次】");
                String base64 = photos.get(i).toString();
                int verifyResult = verifyPhoto(base64);//验证图片可用性
                if ( verifyResult == 1 ){
                   p.setPhoto(base64);
                   //第一步，创建人员图像和将图片转送到服务器，有服务器分发图片到人脸识别设备
                   String setPersonRes = faceApi.setPerson( JSONObject.fromObject(p) );
                   logger.info("【创建人员图像和将图片转送到服务器返回结果：】"+setPersonRes);
                   //第二步，上传图片到服务器进行分发
                   faceApi.uploadRecord(p);
                   return true;
                }else if(verifyResult == 2 ){
                   logger.info("【人脸图像比对第"+i+"次,返回结果：照片已存在】");
                   return true;
                }
            }
        }
        logger.error("拍照响应错误信息"+result);
        return false;


    }




    @Override
    public void setRecordCallback() {

       // String url = "http://"+sfClientIP+":"+sfClientPort+"/face/listRecord";
       // String result = faceApi.setRecordCallback(url);

      //  JSONObject json = JSONObject.fromObject(result);
        /*if( json.getInt("status") == 0 ){
            logger.info("【设置过闸数据自动上传的URL成功】"+url);
        }else{
            logger.error("【设置过闸数据自动上传的URL失败】"+result);
        }*/

    }

    @Override
    public String open() {
        String result = faceApi.open();
        JSONObject json = JSONObject.fromObject(result);
        if( json.getInt("status") == 0 ){
            logger.info("【人脸识别打开闸机成功】");
            return "200";
        }else{
            logger.error("【人脸识别打开闸机失败】");
            return "0";
        }

    }


    /**
     * 验证图片是否可用
     * @param base64
     * @return
     * 1图像可用； 2图像可用，但是已经存在；3图像无效
     */
    private int verifyPhoto(String base64){

        String result = faceApi.check(base64);
        JSONObject json = JSONObject.fromObject(result);
        if( json.getInt("status") == 0 ){
            JSONObject data =JSONObject.fromObject(json.get("data"));
            int isValid = data.getInt("isValid");
            Boolean isExist = data.getBoolean("isExist");
            if(isValid == 0 && isExist ==false ){
                logger.info("【图像可用】"+result);
                return 1;
            }else{
                //"图像不可用或已存在图像"
                if( isExist == true ){
                    logger.info("【系统已经已存在图像】"+result);
                    return 2;
                }else{
                    logger.info("【图像不可用】"+result);
                    return 3;
                }
            }
        }else{
            logger.error("验证照片时候错误信息"+result);
            return 3;
        }
    }



}
